#include "SysSeatInfoDialog.h"
#include "ui_SysSeatInfoDialog.h"
#include <QDebug>
#include <QPushButton>
#include <QGridLayout>
#include <QCheckBox>
#include <QLabel>
#include <QString>
#include <QMessageBox>
#include "SysDAL.h"

static bool DO_Table[140];

SeatInfoDialog::SeatInfoDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SeatInfoDialog)
{
    ui->setupUi(this);
//    SysAssist::Center(this);


}

SeatInfoDialog::~SeatInfoDialog()
{
    delete ui;
}
void SeatInfoDialog::open1()
{
    ui->stackedWidget->setCurrentIndex(0);
    CreatBtn(102);
    this->setWindowTitle(QObject::tr("102座位信息"));
    this->exec();
}
void SeatInfoDialog::open2()
{
    ui->stackedWidget->setCurrentIndex(1);
    CreatBtn(108);
    this->setWindowTitle(QObject::tr("108座位信息"));
    this->exec();
}
void SeatInfoDialog::open3()
{
    ui->stackedWidget->setCurrentIndex(2);
    CreatBtn(601);
    this->setWindowTitle(QObject::tr("601座位信息"));
    this->exec();
}

void SeatInfoDialog::CreatBtn(int ClassId)
{
    QSqlQuery query;
    int rowNum,BtnRowNum=10,BtnColumnNum=14;
    bool power;//数据库权限的数值
    query.setForwardOnly(true);
    bool execOk= query.exec(QString("EXEC PRO_QuerySeatInfo %1").arg(ClassId));

    if(execOk == false)
    {
        QMessageBox::warning(this,QObject::tr("提示"),query.lastError().text());
        qDebug()<<query.lastError().text();
    }
    else
    {
        for (int i=0;i<BtnRowNum;i++)//按钮行数
        {
            for(int j=0;j<BtnColumnNum;j++)//每行按钮个数
            {
                if(query.next())
                {
                    rowNum = query.at()+1;//获取查询结果当前行
                    //将数据库查询结果转换为bool值，query.value()参数，数据库表从左往右从0开始编号，其中3表示权限所在列
                    power = query.value(0).toBool();
                    int count=0;
                    count=count+query.at();
                    //按键名称是 通道1~128，使用QString::asprintf  ui->frame是建立好的一个容器用来确认在窗口中的位置
                    QPushButton *bt = new QPushButton(QString::asprintf("%d", i*BtnColumnNum+j+1), ui->stackedWidget->currentWidget());
                    bt->setGeometry(20+j*30+j*30, i*8+i*45, 40, 40); //4个参数是（X坐标，Y坐标，按键的宽度，按键的高度）
                    if(power==true)
                    {
                        bt->setStyleSheet("QPushButton{background-color:red;border-radius:20px;}");
                    }
                    else if(rowNum==-1)
                    {
                        bt->setText("NULL");
                        bt->setStyleSheet("QPushButton{background-color:gray;border-radius:20px;}");

                    }
                    else
                    {
                        bt->setStyleSheet("QPushButton{background-color:yellow;border-radius:20px;}");
                    }
                    connect(bt,&QPushButton::released,[=](){BT_128(i*BtnColumnNum+j);});//连接到一个函数，函数的参数就是“按键索引”
                    qDebug()<<"第"<<rowNum<<"行："<<power;
                }
            }
        }
                int PushButton_number = ui->stackedWidget->currentWidget()->children().count();//获取创建了多少个按键
                qDebug()<<"PushButton_number"<<PushButton_number;//打印创建的按钮数
    }

}

void SeatInfoDialog::BT_128(int N)
{
    //一个记录状态的数组
    DO_Table[N] = !DO_Table[N];

    //获取所有的按键
    QPushButton *bt = (QPushButton *)(ui->stackedWidget->currentWidget()->children().at(N));
//    qDebug()<<bt->text();//打印按下的按键名称
//    if(DO_Table[N])//按键动态变化
    int CurrentIndexNum=ui->stackedWidget->currentIndex(),id;//获取当前stackedWidget索引值，id表示教室
//    qDebug()<<CurrentIndexNum;
    if(CurrentIndexNum==0)//0表示102教室，1表示108，2表示601
    {

        id=102;
        RegisterInfo(id,bt->text().toInt());
    }
    else if(CurrentIndexNum==1)
    {
        id=108;
        RegisterInfo(id,bt->text().toUInt());
    }
    else if(CurrentIndexNum==2)
    {
        id=601;
        RegisterInfo(id,bt->text().toUInt());

    }
    else{
        QMessageBox::warning(this,"提示","研修室ID号不存在，请联系超级管理员！！\nTel:0851-88888888");
//        qDebug()<<"研修室ID号不存在，请联系超级管理员！！";
    }
}

void SeatInfoDialog::RegisterInfo(int ClassID,int seatNum)
{
    QString StrSql= QString("EXEC PRO_RegisterInfo %1,%2").arg(ClassID).arg(seatNum),StuId,StuName,StuSex,StuMajor,StuClass;
    QSqlQuery query;
    query.exec(StrSql);
    if(query.first())
    {
       StuId = query.value(0).toString();
       StuName = query.value(1).toString();
       StuSex = query.value(2).toString();
       StuMajor = query.value(3).toString();
       StuClass = query.value(4).toString();
       //    qDebug()<<StuId<<StuName<<StuSex<<StuMajor<<StuClass;
       QDialog *dialog = new QDialog(this);
       dialog->setWindowTitle(StuName+QObject::tr("的位置"));
       dialog->setFixedSize(500,350);
       QVBoxLayout *vbox = new QVBoxLayout;
       QLabel *label = new QLabel;
       label->setPixmap(QPixmap("").scaled(90, 90, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
       label->setAlignment(Qt::AlignCenter);
       vbox->addWidget(label);
       label = new QLabel;
       label->setText(QObject::tr("位置：%1教室 %2号座位").arg(ClassID).arg(seatNum));
       label->setAlignment(Qt::AlignCenter);
       label->setStyleSheet("font-size:20px;");
       vbox->addWidget(label);
       label = new QLabel;
       label->setText(QObject::tr("\t\t个人基本信息\n学号：%1\n姓名：%2\n性别：%3\n专业：%4\n班级：%5\n")
                      .arg(StuId).arg(StuName).arg(StuSex).arg(StuMajor).arg(StuClass));
       label->setStyleSheet("font-size:18px;");
       label->setWordWrap(true);
       label->setAlignment(Qt::AlignTop);
       vbox->addWidget(label);
       vbox->addStretch();
       dialog->setLayout(vbox);
       dialog->setWindowModality(Qt::ApplicationModal);//阻塞其他窗体
       dialog->show();
    }
    if(StuId==nullptr)
    {
        QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("该位置暂时无人申请！"));
    }
}
